1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import junit.framework.TestCase;
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  /**
27   * Unit tests for {@link Sets#union}, {@link Sets#intersection} and
28   * {@link Sets#difference}.
29   *
30   * @author Kevin Bourrillion
31   */
32  @GwtCompatible(emulated = true)
33  public class SetOperationsTest extends TestCase {
34  
35    public static class MoreTests extends TestCase {
36      Set<String> friends;
37      Set<String> enemies;
38  
39      @Override public void setUp() {
40        friends = Sets.newHashSet("Tom", "Joe", "Dave");
41        enemies = Sets.newHashSet("Dick", "Harry", "Tom");
42      }
43  
44      public void testUnion() {
45        Set<String> all = Sets.union(friends, enemies);
46        assertEquals(5, all.size());
47  
48        ImmutableSet<String> immut = Sets.union(friends, enemies).immutableCopy();
49        HashSet<String> mut
50            = Sets.union(friends, enemies).copyInto(new HashSet<String>());
51  
52        enemies.add("Buck");
53        assertEquals(6, all.size());
54        assertEquals(5, immut.size());
55        assertEquals(5, mut.size());
56      }
57  
58      public void testIntersection() {
59        Set<String> friends = Sets.newHashSet("Tom", "Joe", "Dave");
60        Set<String> enemies = Sets.newHashSet("Dick", "Harry", "Tom");
61  
62        Set<String> frenemies = Sets.intersection(friends, enemies);
63        assertEquals(1, frenemies.size());
64  
65        ImmutableSet<String> immut
66            = Sets.intersection(friends, enemies).immutableCopy();
67        HashSet<String> mut
68            = Sets.intersection(friends, enemies).copyInto(new HashSet<String>());
69  
70        enemies.add("Joe");
71        assertEquals(2, frenemies.size());
72        assertEquals(1, immut.size());
73        assertEquals(1, mut.size());
74      }
75  
76      public void testDifference() {
77        Set<String> friends = Sets.newHashSet("Tom", "Joe", "Dave");
78        Set<String> enemies = Sets.newHashSet("Dick", "Harry", "Tom");
79  
80        Set<String> goodFriends = Sets.difference(friends, enemies);
81        assertEquals(2, goodFriends.size());
82  
83        ImmutableSet<String> immut
84            = Sets.difference(friends, enemies).immutableCopy();
85        HashSet<String> mut
86            = Sets.difference(friends, enemies).copyInto(new HashSet<String>());
87  
88        enemies.add("Dave");
89        assertEquals(1, goodFriends.size());
90        assertEquals(2, immut.size());
91        assertEquals(2, mut.size());
92      }
93  
94      public void testSymmetricDifference() {
95        Set<String> friends = Sets.newHashSet("Tom", "Joe", "Dave");
96        Set<String> enemies = Sets.newHashSet("Dick", "Harry", "Tom");
97  
98        Set<String> symmetricDifferenceFriendsFirst = Sets.symmetricDifference(
99            friends, enemies);
100       assertEquals(4, symmetricDifferenceFriendsFirst.size());
101 
102       Set<String> symmetricDifferenceEnemiesFirst = Sets.symmetricDifference(
103           enemies, friends);
104       assertEquals(4, symmetricDifferenceEnemiesFirst.size());
105 
106       assertEquals(symmetricDifferenceFriendsFirst,
107           symmetricDifferenceEnemiesFirst);
108 
109       ImmutableSet<String> immut
110           = Sets.symmetricDifference(friends, enemies).immutableCopy();
111       HashSet<String> mut = Sets.symmetricDifference(friends, enemies)
112           .copyInto(new HashSet<String>());
113 
114       enemies.add("Dave");
115       assertEquals(3, symmetricDifferenceFriendsFirst.size());
116       assertEquals(4, immut.size());
117       assertEquals(4, mut.size());
118 
119       immut = Sets.symmetricDifference(enemies, friends).immutableCopy();
120       mut = Sets.symmetricDifference(enemies, friends).
121           copyInto(new HashSet<String>());
122       friends.add("Harry");
123       assertEquals(2, symmetricDifferenceEnemiesFirst.size());
124       assertEquals(3, immut.size());
125       assertEquals(3, mut.size());
126     }
127   }
128 }
129